//
//  ICMTomorrowPrepareViewController.m
//  ICrEAM
//
//  Created by Cty on 2016/11/09.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMTomorrowPrepareViewController.h"
#import "TomorrowPrepareCell.h"
#import "AppointmentPlan.h"
#import "AppointmentToday.h"
#import "GetCarViewController.h"
#import "ICMAppointmentPreviewViewController.h"
#import "AppointmentViewController.h"
#import "ICMAppointmentTodayModel.h"
#import "ICMBasicInfoModel.h"
#import "ICMRepairMaintenanceModel.h"
#import "ICMActivityModel.h"
#import "ICMPartsTopModel.h"
#import "ICMRepairPartsModel.h"
#import "ICMAppointmentOtherInfoModel.h"
#import "ICMAppointmentPlanModel.h"
#import "ICMAppointmentBasicInfoModel.h"


@interface ICMTomorrowPrepareViewController ()<ICMTomorrowPrepareCellDelegate>
@property (nonatomic,strong) UITableView *preTableView;

//页数
@property (nonatomic, assign) int pageNo;

//数据源
@property (nonatomic, strong) ICMAppointmentBasicInfoModel *basicInfoModel;
@property (nonatomic, strong) ICMAppointmentOtherInfoModel *basicOtherModel;
@property (nonatomic, strong) ICMPartsTopModel *partsTopModel;
@property (nonatomic, strong) ICMActivityModel *activityModel;
@property (nonatomic, strong) ICMRepairMaintenancesModel *maintenancesModel;
@property (nonatomic, strong) ICMRepairPartsArrayModel *partsArrayModel;

@end

@implementation ICMTomorrowPrepareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *topBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ActualHeight(40))];
    
    topBar.backgroundColor = Color(0xE5EBEB);
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //明日准备
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(ActualWidth(80), 0, ActualWidth(100), ActualHeight(40))];
    
    title.text = @"明日准备";
    title.textColor = TitleColor;
    title.textAlignment = NSTextAlignmentCenter;
    title.font = NaviFont;
    
    [topBar addSubview:title];
    
    UIView *underLine = [[UIView alloc]initWithFrame:CGRectMake(ActualWidth(80), ActualHeight(39), ActualWidth(100), 1)];
    
    underLine.backgroundColor = TitleColor;
    
    [topBar addSubview:underLine];
    
    [self.view addSubview:topBar];
    [self.view addSubview:self.preTableView];
    
    [self setRefresh];
    
}

//添加上拉加载
- (void)setRefresh {
    self.pageNo = 1;
    [self getTomorrowData];
    
    __weak ICMTomorrowPrepareViewController *my = self;
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        if (self.hasNext) {
            self.pageNo++;
            [my getTomorrowData];
//        } else {
//            [self.listTableView.mj_footer endRefreshingWithNoMoreData];
//        }
    }];
    
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    [footer setTitle:@"正在加载" forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多数据了" forState:MJRefreshStateNoMoreData];
    self.preTableView.mj_footer = footer;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSMutableArray *)dataSource{
    
    if(!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;

}

- (UITableView *)preTableView{
    
    if(!_preTableView){
        _preTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, ActualHeight(40), ScreenWidth, ScreenHeight-ActualHeight(40))];
        _preTableView.delegate = self;
        _preTableView.dataSource = self;
        _preTableView.backgroundColor = BackgroundColor_White;
        [_preTableView registerClass:[TomorrowPrepareCell class] forCellReuseIdentifier:@"TomorrowPrepareCell"];
        _preTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
        _preTableView.tableFooterView.backgroundColor = [UIColor whiteColor];
    }
    
    return _preTableView;
    
}

#pragma mark  /*tableViewDelegate tableViewDataSource*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ICMAppointmentPlanModel *model = self.dataSource[indexPath.row];
    if([model.CAN_EDIT isEqualToString:@"12781002"] && [ICrEAMTool isVW]){
        [self.view showString:@"微信的预约单，不允许编辑!" forSeconds:1.5];
        
        return;
    }

    [self didClickCellWithRECEPTION_NO:model.BOOKING_ORDER_NO type:@"0"];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TomorrowPrepareCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TomorrowPrepareCell"];
    if (!cell) {
        cell = [[TomorrowPrepareCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TomorrowPrepareCell"];
    }
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell updateDataWithModel:[self.dataSource objectAtIndex:indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ActualHeight(95);
}

#pragma mark -- 点击预览
- (void)didClickPreViewWithIndex:(NSIndexPath *)index{
    ICMAppointmentPlanModel *model = self.dataSource[index.row];
    [self didClickCellWithRECEPTION_NO:model.BOOKING_ORDER_NO type:@"1"];
}

- (void)getTomorrowData {
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:@"" forKey:@"QUERY_CONDITION"];
    [parameters setObject:ICMEnumIntToString(ICMEnumAppointmentTypePlan) forKey:@"QUERY_TYPE"]; //1101当天  1102 未来计划
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *tomorrow = [formatter stringFromDate:[[NSDate alloc] initWithTimeIntervalSinceNow:24*60*60]];
    [parameters setObject:tomorrow forKey:@"QUERY_START_DATE"];
    [parameters setObject:tomorrow forKey:@"QUERY_END_DATE"];
    [parameters setObject:@(self.pageNo) forKey:@"CURRENT_PAGE"];
    [manager startGetRequestWithURL:QueryBookingOrder parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            if (IsArr(data[@"TT_BOOKING_OEDER"])) {
                NSArray *dataArray = data[@"TT_BOOKING_OEDER"];
                if (dataArray.count != 0 && dataArray) {
                    for (NSDictionary *searchData in dataArray) {
                        ICMAppointmentPlanModel * model = [ICMAppointmentPlanModel yy_modelWithDictionary:searchData];
                        [self.dataSource addObject:model];
                        [self.preTableView.mj_footer endRefreshing];
                    }
                } else {
                    if (self.dataSource.count == 0) {
                        self.preTableView.mj_footer.hidden = YES;
                        [self.view showString:@"暂无相关搜索结果！" forSeconds:1.5];
                    } else {
                        [self.preTableView.mj_footer endRefreshingWithNoMoreData];
                    }
                }
            } else {
                if (self.dataSource.count == 0) {
                    self.preTableView.mj_footer.hidden = YES;
                    [self.view showString:@"暂无相关搜索结果！" forSeconds:1.5];
                } else {
                    [self.preTableView.mj_footer endRefreshingWithNoMoreData];
                }
            }
            [self.preTableView reloadData];
        } else {
            [self.preTableView.mj_footer endRefreshing];
        }
        
    }];
}
//type : 0详情 1预览
- (void)didClickCellWithRECEPTION_NO:(NSString *)RECEPTION_NO type:(NSString *)type{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:RECEPTION_NO forKey:@"ORDER_ID"];
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    [manager startGetRequestWithURL:BookingOrderDetils parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            if (IsArr(data[@"TT_BOOKING_OEDER"])) {
                self.basicInfoModel = [ICMAppointmentBasicInfoModel yy_modelWithDictionary:data[@"TT_BOOKING_OEDER"][0]];
                self.basicOtherModel = [ICMAppointmentOtherInfoModel yy_modelWithDictionary:data[@"TT_BOOKING_OEDER"][0]];
                self.partsTopModel = [ICMPartsTopModel yy_modelWithDictionary:data[@"TT_BOOKING_OEDER"][0]];
            }
            self.maintenancesModel = [ICMRepairMaintenancesModel yy_modelWithDictionary:data];
            [self.maintenancesModel createRepairMaintenanceBookingModel];
            
            self.partsArrayModel = [ICMRepairPartsArrayModel yy_modelWithDictionary:data];
            [self.partsArrayModel createRepairPartsBookingModel];
            
            self.activityModel = [ICMActivityModel yy_modelWithDictionary:data];
            [self.activityModel createActivityItemBookingModel];
            
            if ([type isEqualToString:@"1"]){
                ICMAppointmentPreviewViewController *PVC = [[ICMAppointmentPreviewViewController alloc]init];
                PVC.basicInfoModel = self.basicInfoModel;
                PVC.basicOtherModel = self.basicOtherModel;
                PVC.partsTopModel = self.partsTopModel;
                PVC.maintenancesModel = self.maintenancesModel;
                PVC.partsArrayModel = self.partsArrayModel;
                PVC.activityModel = self.activityModel;
                [self.navigationController pushViewController:PVC animated:YES];
            }else{
                AppointmentViewController *appointmentViewController =[AppointmentViewController new];
                appointmentViewController.isEditType = YES;
                appointmentViewController.basicInfoModel = self.basicInfoModel;
                appointmentViewController.basicOtherModel = self.basicOtherModel;
                appointmentViewController.partsTopModel = self.partsTopModel;
                appointmentViewController.maintenancesModel = self.maintenancesModel;
                appointmentViewController.partsArrayModel = self.partsArrayModel;
                [self.navigationController pushViewController:appointmentViewController animated:YES];
            }
        }
    }];
}

@end
